const checkRequest = async (ctx, rules) => {
	// 获取参数
	const args = ctx.request.body;
	let warnings = [];
	for (const rule of rules) {
		if (!args[rule.field]) {
			warnings.push({
				field: rule.field,
				type: rule.type,
				message: `${rule.field}不能为空`
			});
		} else {
			if (typeof args[rule.field] !== rule.type) {
				warnings.push({
					field: rule.field,
					type: rule.type,
					message: `${rule.field}必须是${rule.type}类型`
				});
			}
		}
	}

	if (warnings.length) {
		ctx.body = {
			code: 400,
			message: "参数错误",
			data: warnings
		};
	}

	return !!warnings.length;
};

module.exports = checkRequest;
